﻿var mix = (function (mix) {

mix.rect2d = {};
var rect2d = mix.rect2d;

var Rect2D = function( x, y, w, h) {
    this.x = x;
    this.y = y;
    this.w = w;
    this.h = h;
}

Rect2D.prototype = {
    getRight : function() {
        return this.x + this.w;
    },
    
    getBottom : function() {
        return this.y + this.h;
    },
}

rect2d.create = function( x, y, w, h) {
    return new Rect2D(x, y, w, h);
}

// 两中心坐标之差小于等于宽高一半之和
rect2d.intersect = function( r1, r2) {
    if ( Math.abs( r1.x + r1.x + r1.w - (r2.x + r2.x + r2.w)) <= (r1.w + r2.w))
        if ( Math.abs( r1.y + r1.y + r1.h - (r2.y + r2.y + r2.h)) <= (r1.h + r2.h))
            return true;
    return false;
}

rect2d.interPoint = function( r, p) {
    if ( p.x >= r.x && p.x <= (r.x + r.w) && p.y >= r.y && p.y <= (r.y + r.h))
        return true;
    return false;
}

rect2d.interArea = function( r1, r2, r) {
    if ( !r)
        r = rect2d.create(0, 0, 0, 0);
        
    r.x = Math.max( r1.x, r2.x);
    r.y = Math.max( r1.y, r2.y);
    var right = Math.min( (r1.x + r1.w), (r2.x + r2.w));
    var bottom = Math.min( (r2.y + r2.h), (r2.y + r2.h));
    if ( r.x <= right && r.y <= bottom) {
        r.w = right - r.x;
        r.h = right - r.y;
        return r;
    }
    return null;
}

return mix;           
      
}(mix || {}));

